package com.example.gridviewandarrayadapter;

import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.gridviewandarrayadapter.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gv=(GridView) findViewById(R.id.gridView1);
        final String[] c=new String[]{"doraemon","dora1","dora2","dora3","dora4","dora5","dora6",};
        final int[] img=new int[]{R.mipmap.doraemon,R.mipmap.dora1,R.mipmap.dora2,R.mipmap.dora3,R.mipmap.dora4,
                R.mipmap.dora5,R.mipmap.dora6,};
        BaseAdapter adapter=new BaseAdapter() {
            @Override
            public int getCount() {
                return img.length;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {
                View v= LayoutInflater.from(MainActivity.this).inflate(R.layout.item,null);
                ImageView image=(ImageView) v.findViewById(R.id.imageView);
                TextView name=(TextView) v.findViewById(R.id.textView);
                image.setImageResource(img[arg0]);
                name.setText(c[arg0]);
                return v;
            }
        };
        GridView gridView=(GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Toast.makeText(MainActivity.this,c[arg2],
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }

}